<?php
namespace View;
//Vy för kommentarer
class CommentView {
	private $commentsID = 'commentid';
	private $commentID = '';
	private $name = 'name';
	private $comment = 'comment';
	private $submitComment = 'submitComment';
	private $deleteComment = 'deletecomment';

	const COMMENT_EMPTY = 0;
	const COMMENT_SUCCESS = 1;
	const COMMENT_FAILED = 2;

	//Visar alla commentarer för vald inlägg
	public function ShowListOfComments(\Model\CommentArray $commentArray, $database) {
		$ret = "";
		$deletebutton = "";
		$loginhandler = new \Model\LoginHandler($database);

		foreach ($commentArray->get() as $comments) {
			$ret .= "<div class='post'>";
			$ret .= "<h2 class='title'>" . $comments -> getName() . "</h2>";
			$ret .= "<div class='entry'> <p>" . $comments -> getComment() . "</p></div>";
			$ret .= "<div class='byline'>";
			$ret .= "<p class='meta'>" . $comments -> getDate() . "</p>";
			$ret .= "</br>";
			if ($loginhandler -> IsLoggedIn() == true) {
				$deletebutton = $this -> DeleteButton($comments -> getCommentId());
			}

			$ret .= "</br>" . $deletebutton;
			$ret .= "</div>";
			$ret .= "</div>";
		}
		return $ret;
	}

	//Ny kommentar
	public function NewCommentForm() {

		$xhtml = "
		<h3>Ny kommentar:</h3>
			<form action='' method='post' class='post'> 
				<p>Namn:</p><input type='text' name='$this->name' style='width:250px; height:30px; '/></br>
				<p>Kommentar:</p><textarea type='text' name='$this->comment' style='width:550px; height:120px;' ></textarea></br>
				<input type='submit' name='$this->submitComment' value='Skapa' class='delete' style='margin-top:10px;'/>
				</form>";

		return $xhtml;
	}

	//Radera kommentar knapp, sparar comment id i en hidden input
	public function DeleteButton($commentid) {
		$this -> commentID = $commentid;
		$xhtml = "
		<div>
		<form action='' method='post'> 
			<input type='hidden' name='$this->commentsID' value='$this->commentID' />
			<input type='submit' name='$this->deleteComment' value='Radera' class='delete'/>
		</form>
		</div>";

		return $xhtml;

	}

	public function CommentID() {
		if (isset($_POST[$this -> commentsID]) == true) {
			return $_POST[$this -> commentsID];
		}
	}

	public function Name() {
		if (isset($_POST[$this -> name]) == true) {
			return $_POST[$this -> name];
		}
		return null;

	}

	public function Comment() {
		if (isset($_POST[$this -> comment]) == true) {
			return $_POST[$this -> comment];
		}
		return null;
	}

	public function TriedToSubmit() {
		if (isset($_POST[$this -> submitComment]) == true) {
			return true;
		}
		return false;
	}

	public function TriedToDelete() {
		if (isset($_POST[$this -> deleteComment]) == true) {
			return true;
		}
		return false;
	}

	public function NewCommentMess($status) {
		switch ($status) {
			case self::COMMENT_EMPTY :
				return "<p class='warning'>Titel eller inlägget är tomt!</p>";
				break;
			case self::COMMENT_SUCCESS :
				return "<p class='Success'>Ditt inlägg sparades!</p>";
				break;
			case self::COMMENT_FAILED :
				return "<p class='warning'>Ditt inlägg gick inte att spara!</p>";
				break;
			default;
				break;
		}
	}

}
